package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.mappers.PmsProductMapper;
import com.mall.dubbo.mappers.SmsFlashPromotionProductRelationMapper;
import com.tanhua.model.domain.PmsProduct;
import com.tanhua.model.domain.SmsFlashPromotionProductRelation;
import com.tanhua.model.vo.FlashProductRelationListVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsFlashPromotionProductRelationApiImpl implements SmsFlashPromotionProductRelationApi {

    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;

    @Autowired
    private PmsProductMapper pmsProductMapper;

    /**
     * 分页查询不同场次关联及商品信息
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param page
     * @return
     */
    @Override
    public Page list(Integer flashPromotionId, Integer flashPromotionSessionId, Page<SmsFlashPromotionProductRelation> page) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId)
                .eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId)
                .orderByDesc(SmsFlashPromotionProductRelation::getSort);
        Page<SmsFlashPromotionProductRelation> pageList = smsFlashPromotionProductRelationMapper.selectPage(page, lambdaQueryWrapper);

        List<SmsFlashPromotionProductRelation> records = pageList.getRecords();

        List<FlashProductRelationListVo> resultList = records.stream().map(s -> {
            FlashProductRelationListVo flashProductRelationListVo = new FlashProductRelationListVo();
            BeanUtils.copyProperties(s, flashProductRelationListVo);

            Long productId = s.getProductId();
            if (productId != null) {
                PmsProduct pmsProduct = pmsProductMapper.selectById(productId);
                flashProductRelationListVo.setProduct(pmsProduct);
            }
            return flashProductRelationListVo;
        }).collect(Collectors.toList());


        Page<FlashProductRelationListVo> resultPage = new Page<>();

        BeanUtils.copyProperties(pageList,resultPage,"records");

        return resultPage.setRecords(resultList);
    }

    /**
     * 修改关联信息
     * @param smsFlashPromotionProductRelation
     * @param smsFlashPromotionProductRelation
     * @return
     */
    @Override
    public void updateFlashProductRelation(SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        smsFlashPromotionProductRelationMapper.updateById(smsFlashPromotionProductRelation);
    }

    /**
     * 删除关联
     * @param id
     */
    @Override
    public void deleteFlashProductRelation(Long id) {
        smsFlashPromotionProductRelationMapper.deleteById(id);
    }
    /**
     * 批量选择商品添加关联
     * @param relationList
     * @return
     */
    @Override
    public void createFlashProductRelation(List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : relationList) {

            PmsProduct pmsProduct = pmsProductMapper.selectById(smsFlashPromotionProductRelation.getProductId());

            //设置默认秒杀价格,等于原价
            smsFlashPromotionProductRelation.setFlashPromotionPrice(pmsProduct.getPrice());

            smsFlashPromotionProductRelation.setFlashPromotionCount(0);
            smsFlashPromotionProductRelation.setFlashPromotionLimit(0);
            smsFlashPromotionProductRelationMapper.insert(smsFlashPromotionProductRelation);
        }
    }


}
